import {
    _decorator,
    Component,
    Layout,
    Prefab,
    instantiate,
    Label,
    Button,
    director,
    Node
} from "cc";
import Long from "long";
import {PropItem} from "db://assets/script/game/mainui/props/prop-item";
import {GameEvent} from "db://assets/script/game-event";
import {UserFishReceiveBagEvent} from "db://assets/script/common/events/fish-events/user-fish-receive-bag-event";
import {userInfo} from "db://assets/script/user-info";
import {PropDesc} from "db://assets/script/game/mainui/props/prop-desc";
import {lobby} from "db://assets/script/lobby";
import {BagType} from "db://assets/script/game/constants/bag-type";
import {configsHelper} from "db://assets/script/configs-helper";
import {fishUtils} from "db://assets/script/utils/fish-utils";
import {fishConfigsLoader} from "db://assets/script/configs/fish-configs-loader";
import {propsConfigsLoader} from "db://assets/script/configs/props-configs-loader";

const {ccclass, property} = _decorator;

@ccclass('PropBag')
export class PropBag extends Component {

    @property(Layout)
    propBox!: Layout;

    @property(Prefab)
    propItemPrefab!: Prefab;

    @property(Label)
    title!: Label

    @property(Button)
    receiveBtn!: Button

    @property(Node)
    fishReceiveTips!: Node

    @property(Prefab)
    tipsPrefab!: Prefab;

    fishId: Long | null = null;

    bagType!: BagType;

    propItems: { [k: number]: PropItem } = {}


    start() {
        this.receiveBtn.node.on(Button.EventType.CLICK, this._receiveFishBag, this)
    }


    showAssetsBag(bagType: BagType, assets: { [key: number]: Long }, fishId: Long | null) {
        this.bagType = bagType
        this.propBox.node.destroyAllChildren()
        this.propItems = {}
        if (fishId) {
            const ff = userInfo.fishInfo.loadFishById(fishId)
            if (ff) {
                const alive = fishUtils.checkFishAlive(ff.status)
                this.receiveBtn.node.active = !alive
                this.fishReceiveTips.active = alive
            } else {
                this.receiveBtn.node.active = false
                this.fishReceiveTips.active = false
            }
        } else {
            this.receiveBtn.node.active = false
            this.fishReceiveTips.active = false
        }
        this.title.string = configsHelper.loadPropBagTitle(bagType)!
        this.fishId = fishId
        for (let assetId in assets) {
            const ac = propsConfigsLoader.findPropConfigs(Number(assetId))
            if (!ac) continue
            if (ac.showInBag.indexOf(this.bagType.valueOf()) == -1) continue
            const num = assets[assetId]
            if (num.lte(0)) continue
            this._newPropItem(Number(assetId), num)
        }
    }

    changeAssetsNum(assets: { [key: number]: Long }) {
        for (let assetId in assets) {
            const ac = configsHelper.loadAssetConfig(Number(assetId))
            if (!ac) continue
            if (ac.showInBag.indexOf(this.bagType.valueOf()) == -1) continue
            const num = assets[assetId]
            if (this.propItems && this.propItems[assetId]) {
                if (num.toNumber() == 0) {
                    this.propItems[assetId].node.destroy()
                    delete this.propItems[assetId]
                } else {
                    this.propItems[assetId].changeNum(num.toNumber())
                }
            } else {
                if (num.toNumber() == 0) continue
                this._newPropItem(Number(assetId), num)
            }

        }
    }

    clearBag(): void {
        this.propBox.node.destroyAllChildren()
        this.propItems = {}
    }

    private _newPropItem(assetId: number, num: Long) {
        const pi = instantiate(this.propItemPrefab)
        const propItem = pi.getComponent(PropItem)!
        propItem.init(assetId, num.toNumber())
        this.propItems[assetId] = propItem
        this.propBox.node.addChild(pi)
        propItem.propIcon.node.on(Node.EventType.TOUCH_END, () => {
            this.showAssetTips(pi, assetId)
        })
    }

    showAssetTips(targetNode: Node, assetId: number) {
        const tn = instantiate(this.tipsPrefab)
        lobby.instance.gui.addChild(tn)
        const propDesc = tn.getComponent(PropDesc)!
        propDesc.init(targetNode, assetId)
    }

    _receiveFishBag() {
        if (this.fishId) {
            const ff = userInfo.fishInfo.loadFishById(this.fishId)
            if (ff) {
                director.emit(GameEvent.userFishRequestReceiveFishBag, new UserFishReceiveBagEvent(ff.id, ff.zoneId.toString(), this, () => {
                }))
            }
        }

    }


}